<?php


namespace app\common\controller;


use app\BaseController;
use app\common\model\AdminRole;
use app\common\model\Route;

class Backstage extends BaseController
{
    /**
     * 控制器中间件 后台用
     * @var array
     */
    protected $middleware = [
        'app\\middleware\\Admin'
    ];

    /**
     * 获取管理系统左侧功能列表
     * @param $role_id
     */
    protected function roleList($role_id)
    {
        $list = [];
        $route = new Route();
        if ($role_id == 0) {//超级管理员
            $where = env('APP_DEBUG') == 1 ? [] : "id != 1";
            $list = $route
                ->where($where)
                ->where(['is_menu' => 1])
                ->order("weigh DESC")
                ->select()->toArray();
            $list = make_tree($list);
        } else {//找到和自己账号匹配的左侧栏目
            $admin_role = new AdminRole();
            $role_id = $admin_role->where(['id' => $role_id])->find();
            if (empty($role_id->role_id)) {
                return $list;
            } else {
                $list = $route
                    ->where([
                        ['id', '<>', 1],
                        ['is_menu', '=', 1],
                        ['id', 'in', $role_id->role_id],
                    ])
                    ->order("weigh DESC")
                    ->select()
                    ->toArray();
                $list = make_tree($list);
            }
        }
        return $list;
    }


    //递归找到所有儿子孙子一维数组  $mid父级id  $level从几级开始
    protected function get_downline($members, $mid, $level = 0)
    {
        $arr = [];
        foreach ($members as $key => $v) {
            if ($v['pid'] == $mid) {  //pid为0的是顶级分类
                $v['level'] = $level + 1;
                $arr[] = $v;
                $arr = array_merge($arr, $this->get_downline($members, $v['id'], $level + 1));
            }
        }
        return $arr;
    }

}
